import 'dart:js_interop';

import 'package:flutter_enjoy_plus/constants/index.dart';
import 'package:flutter_enjoy_plus/utils/Toast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class TokenManager {
  //  token管理
  String _token = '';

  // 获取首选项实例
  Future<SharedPreferences> getTokenManager() {
    return SharedPreferences.getInstance();
  }

  // 初始化token
  initToken() async {
    final prefs = await getTokenManager();
    _token = prefs.getString(GlobalVariable.TOKEN_KEY) ?? '';
  }

  // 存储token
  setToken(String token) async {
    final prefs = await getTokenManager();
    prefs.setString(GlobalVariable.TOKEN_KEY, token);
    _token = token;
  }

  // 获取token
  getToken() {
    return _token;
  }

  // 删除token
  removeToken() async {
    final prefs = await getTokenManager();
    prefs.remove(GlobalVariable.TOKEN_KEY);
    _token = '';
  }
}

TokenManager tokenManager = TokenManager();
